package com.school.manager.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.school.manager.common.Constants;
import com.school.manager.domaim.LifeSchool;
import com.school.manager.domaim.SysUser;
import com.school.manager.entity.ResponseEntity;
import com.school.manager.entity.po.LifeSchoolPo;
import com.school.manager.entity.vo.LifeSchoolVo;
import com.school.manager.enums.ResponseEnum;
import com.school.manager.service.ILifeSchoolService;
import com.school.manager.service.ISysUserService;
import com.school.manager.utils.ConvertUtil;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;

/**
 * description：生活院校控制器
 * date：2021/10/133:18 下午
 */

@RestController
@Api(tags = "生活院校")
@RequestMapping(Constants.PROGRAM_PATH + "lifeschool")
public class LifeSchoolController {

	@Autowired
    private ILifeSchoolService lifeSchoolService;
	
	@Autowired
	private ISysUserService userService;
	
	@PostMapping("add")
    @ApiOperation("新增生活院校")
    @ApiImplicitParam(name = "生活院校", value = "lifeSchoolPo", dataTypeClass = LifeSchoolPo.class)
    public ResponseEntity add(@RequestBody LifeSchoolPo lifeSchoolPo) {
        return ResponseEnum.SUCCESS.success(lifeSchoolService.save(ConvertUtil.convertObj(lifeSchoolPo, LifeSchool.class)));
    }


    @PostMapping("edit")
    @ApiOperation("修改生活院校")
    @ApiImplicitParam(name = "生活院校", value = "lifeSchoolPo", dataTypeClass = LifeSchoolPo.class)
    public ResponseEntity edit(@RequestBody LifeSchoolPo lifeSchoolPo) {
    	SysUser sysUser = userService.getById(lifeSchoolPo.getUserId());
    	lifeSchoolPo.setUserName(sysUser.getName());
        return ResponseEnum.SUCCESS.success(lifeSchoolService.updateById(ConvertUtil.convertObj(lifeSchoolPo, LifeSchool.class)));
    }


    @GetMapping("get/{id}")
    @ApiOperation("生活院校详情")
    @ApiImplicitParam(name = "生活院校主键", value = "id", dataTypeClass = String.class)
    public ResponseEntity get(@PathVariable("id") String id) {
    	LifeSchool lifeSchool = lifeSchoolService.getById(id);
        return ResponseEnum.SUCCESS.success(ConvertUtil.convertObj(lifeSchool, LifeSchoolVo.class));
    }


    @PostMapping("page")
    @ApiOperation("生活院校分页列表")
    @ApiImplicitParam(name = "生活院校Po", value = "lifeSchoolPo", dataTypeClass = LifeSchoolPo.class)
    public ResponseEntity page(@RequestBody LifeSchoolPo lifeSchoolPo) {
        return ResponseEnum.SUCCESS.success(lifeSchoolService.page(lifeSchoolPo));
    }
}
